package com.yingsheng.bi.hks.web.controller;

import com.yingsheng.bi.hks.basic.consts.NameDict;
import com.yingsheng.bi.hks.basic.utils.HttpUtils;
import com.yingsheng.bi.hks.basic.utils.JwtUtils;
import com.yingsheng.bi.hks.basic.utils.Subject;
import com.yingsheng.bi.hks.dto.HksTransReq;
import com.yingsheng.bi.hks.dto.HksTransRes;
import com.yingsheng.bi.hks.dto.user.BaseInfoModReq;
import com.yingsheng.bi.hks.dto.user.BaseInfoModRes;
import com.yingsheng.bi.hks.dto.user.UserLoginRes;
import com.yingsheng.bi.hks.dto.user.WxAuthLoginReq;
import com.yingsheng.bi.hks.feign.client.UserFeignClient;
import com.yingsheng.bi.hks.service.user.LoginBiService;
import com.yingsheng.bi.hks.service.user.UsersMgmtService;
import com.yingsheng.bi.hks.template.annotation.Template;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RestController;

/**
 * 用户服务
 *
 * @author root
 */
@RestController
public class UserCtrl implements UserFeignClient {

    @Autowired
    private LoginBiService loginBiService;

    @Autowired
    private UsersMgmtService usersMgmtService;

    @Template(name = "publicTransTemplate")
    @Override
    public UserLoginRes process(@Validated WxAuthLoginReq req) {
        UserLoginRes res = loginBiService.process(req);
        String loginToken = JwtUtils.loginToken(req.getChannel(), res.getUserId());
        HttpUtils.setResponseHeader(NameDict.ACCESS_TOKEN, loginToken);
        return res;
    }

    @Template(name = "accessTransTemplate")
    @Override
    public HksTransRes process(HksTransReq req) {
        Subject.del();
        return new HksTransRes();
    }

    @Template(name = "accessTransTemplate")
    @Override
    public BaseInfoModRes process(@Validated BaseInfoModReq req) {
        return usersMgmtService.baseInfoMod(req);
    }

}
